// pages/classify/classify.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num: 0,//顶部tab 当前选中下标

    cate: [],//顶部tab 数据,
    goodslist: [],//商品数据
    page_num: 1,//页码
    bid: '',
    tid: ''
  },
  focus_click: function (e) {
    let tid = e.currentTarget.id
    let _num = e.currentTarget.dataset.index;

    console.log(tid)
    this.setData({
      page_num: 1,
      tid,
    });

    this.ajax_get_goods_data(this.data.page_num).then(res => {
      console.log(res)
      this.setData({
        goodslist: res.data.data.goods,
        _num
      })
    })
    this.goTop()
  },
  search: function () {
    wx.navigateTo({
      url: '../search/seller',
    })
  },

  onshop: function (e) {
    let that = this;
    var goodslist = that.data.goodslist;
    var tid = e.currentTarget.dataset.tid
    app.navTo('../normal/normal?id=' + goodslist[e.currentTarget.dataset.index].goodsid + '&tid=' + tid)
  },

  ajax_get_goods_data(page_num) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.data.Url + 'goods/lists',
        method: 'post',
        data: {
          bid: app.data.address.id,
          tid: this.data.tid,
          page: page_num
        },
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },
  load_more_data(){
    this.setData({
      page_num: ++this.data.page_num
    });
    this.ajax_get_goods_data(this.data.page_num).then(res=>{
      if(res.data.data.goods.length==0){
        wx.showToast({
          title: '没有更多了', 
          icon:'none'
        });
        return;
      }
      this.setData({
        goodslist:this.data.goodslist.concat(res.data.data.goods)
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    var that = this;

    wx.request({
      url: app.data.Url + 'menus/menus',
      data: {
        bid: app.data.address.id
      },
      method: 'get',
      success: function (res) {

        that.setData({
          cate: res.data.data
        })
        init_goods_func()
      }
    })
    let init_goods_func = () => {


      let tid = this.data.cate[0].tid;
      this.setData({
        tid
      })
      this.ajax_get_goods_data(this.data.page_num).then(res => {
        console.log("---------")
        console.log(res);
        this.setData({
          goodslist: res.data.data.goods
        })
      })
    }




  },
  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
 
  onReachBottom(){
    this.load_more_data()
  }


})


/**
 * 
 * 
 * 
 * wx.request({
              url: app.data.Url + 'goods/lists',
              data: {
                // bid:123,
                bid: app.data.address.id,
                tid: res.data.data[0].tid,
                page: 1
              },
              method: 'post',
              success: function (res) {
                console.log(res)
                that.setData({
                  goodslist: res.data.data.goods,
                  care: res.data.data,
                  _num: 0
                })
                wx.hideLoading()

              }
            })
 */